package com.cs543.j.client.cloudService.storage;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONStringer;
import org.json.JSONTokener;


import android.util.Log;

import com.cs543.j.client.cloudService.CloudRequestCode;
import com.cs543.j.client.comm.RemoteCloudCommunication;

public class SendRPCMessage implements Runnable {

	String URL;
	CloudRequestCode code;
	List parameter;
	int requestID;
	OnRPCFinishListener listener;

	public SendRPCMessage(String URL, CloudRequestCode code, List parameter, int requestID, OnRPCFinishListener listener) {
		super();
		this.URL = URL;
		this.code = code;
		this.requestID = requestID;
		this.listener = listener;
		this.parameter = parameter;
	}

	public void run() {
		RemoteCloudCommunication remote = new RemoteCloudCommunication(this.URL);

		if (CloudRequestCode.LOOKUP.equals(code)) {
			Log.i("SendRPCMessage", "Lookup : " + (String) parameter.get(0));
			String result = remote.lookup((String) parameter.get(0));
			Log.i("SendRPCMessage", result);

			JSONArray jsonObject = null;
			try {
				jsonObject = new JSONArray(result);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			this.listener.onFinish(jsonObject);
		} else if (CloudRequestCode.UPLOAD.equals(code)) {
			String data = (String) parameter.get(1);
			Log.i("SendRPCMessage", "Upload path : " + (String) parameter.get(0));
			Log.i("SendRPCMessage", "Upload file : " + data.substring(0, Math.min(data.length(), 100)));
			String result = remote.upload((String) parameter.get(0), (String) parameter.get(1));
			Log.i("SendRPCMessage", "Upload result : " + result);
		}else if (CloudRequestCode.READ.equals(code)) {
			Log.i("SendRPCMessage", "Download path : " + (String) parameter.get(0));
			String result = remote.download((String) parameter.get(0), null);
			
			this.listener.onFinish(result);
		}else if (CloudRequestCode.DELETE.equals(code)) {
			Log.i("SendRPCMessage", "Delete path : " + (String) parameter.get(0));
			String result = remote.delete((String) parameter.get(0));
		}else if (CloudRequestCode.MOVE.equals(code)) {
			Log.i("SendRPCMessage", "Move path 1 : " + (String) parameter.get(0));
			Log.i("SendRPCMessage", "Move path 2 : " + (String) parameter.get(1));
			String result = remote.move((String) parameter.get(0), (String) parameter.get(1));
			Log.i("SendRPCMessage", "Move result : " + result);
		}else if (CloudRequestCode.COPY.equals(code)) {
			Log.i("SendRPCMessage", "Copy path 1 : " + (String) parameter.get(0));
			Log.i("SendRPCMessage", "Copy target client : " + (String) parameter.get(1));
			Log.i("SendRPCMessage", "Copy path 2 : " + (String) parameter.get(2));
			String result = remote.copy((String) parameter.get(0), (String) parameter.get(1), (String) parameter.get(2));
			Log.i("SendRPCMessage", "Move result : " + result);
		}
		
	}

	public void start() {
		new Thread(this).start();
	}
}
